package com.hckj.jianzhijia.networks;

import android.os.AsyncTask;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.hckj.jianzhijia.model.City;
import com.hckj.jianzhijia.model.Province;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 赵奇隆 on 2015-11-28.
 */
public class GetCityNetWork extends AsyncTask<Void,Void,ArrayList<City>> {

    private int provinceId = -1;
    private NetWorkInterface netWorkInterface;


    public GetCityNetWork(int provinceId,NetWorkInterface netWorkInterface) {
        this.provinceId = provinceId;
        this.netWorkInterface = netWorkInterface;
    }

    @Override
    protected ArrayList<City> doInBackground(Void... params) {

        AVQuery avObjectAVQuery = AVQuery.getQuery(City.TABLE_NAME);
        avObjectAVQuery.whereEqualTo("belongTo",provinceId);
        ArrayList<City> cities = new ArrayList<City>();
        try {
            List list = avObjectAVQuery.find();
            for(int i=0,j=list.size();i<j;i++){
                cities.add(new City((AVObject)(list.get(i))));
            }
            return cities;
        } catch (AVException e) {
            return null;
        }

    }

    @Override
    protected void onPostExecute(ArrayList<City> cities) {
        netWorkInterface.afterGetCities(cities);
        super.onPostExecute(cities);
    }

    public interface NetWorkInterface{
        public void afterGetCities(ArrayList<City> cities );
    }
}
